package cn.imvc.app.controllers.info;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.controllers.goods.GoodsControllerImpl;
import cn.imvc.app.entities.Activity;
import cn.imvc.app.entities.Red;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.RedModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class RedControllerImpl implements RedController{
	static Logger logger = Logger.getLogger(RedControllerImpl.class);
	public Page<Red> page;
	public ViewRequest viewRequest;
	public Red red;
	public int id;
	public int theAmount;
	@Autowired
	RedModel redModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {
		return null;
	}

	public String index() {
		if(viewRequest == null){
			viewRequest = new ViewRequest();
		}
		page = redModel.find(viewRequest);
		return "red/index";
	}

	public String show() {
		return null;
	}

	public String editNew() {
		return "red/edit";
	}

	public String create() {
		red.setAmount(theAmount*100);
		int result = redModel.save(red);
		if(result==0){
			return Render.json(new DialogBean("300","金额不足或每个红包领取人数超标!","","closeCurrent"));
		}else if(result==-1){
			return Render.json(new DialogBean("300","每个红包领取人数不能小于3人!","","closeCurrent"));
		}else{
			loggingModel.info(logger,"添加红包："+red.getTitle());
		    return Render.json(new DialogBean("200","添加成功","","closeCurrent"));
		}
	}

	public String edit() {
		red = redModel.get(id);
		return "red/edit";
	}

	public String update() {
		red.setAmount(theAmount*100);
		int result = redModel.update(id,red);
		if(result==0){
			return Render.json(new DialogBean("300","金额不足或每个红包领取人数超标!","","closeCurrent"));
		}else if(result==-1){
			return Render.json(new DialogBean("300","每个红包领取人数不能小于3人!","","closeCurrent"));
		}else{
			loggingModel.info(logger,"修改红包："+red.getTitle());
		    return Render.json(new DialogBean("200","修改成功","","closeCurrent"));
		}
	}

	public String destroy() {
		redModel.delete(id);
		loggingModel.info(logger,"删除红包："+id);
		return Render.json(new DialogBean("200","删除成功",""));
	}

	public String destroyBatch() {
		return null;
	}

	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public Red getRed() {
		return red;
	}

	public void setRed(Red red) {
		this.red = red;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getTheAmount() {
		return theAmount;
	}

	public void setTheAmount(int theAmount) {
		this.theAmount = theAmount;
	}

}
